/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.Product;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;

/**
 *
 * @author Hien-0912141
 */
public class ProductDAO {
    
    public static List<Product> getListProduct() {
        List<Product> listProduct = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        try {
            String hql = "from Product";
            Query query = session.createQuery(hql);
            listProduct = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return listProduct;
    }
    
    public static Product getProductById(int productId) {
        Product product = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            product = (Product)session.get(Product.class, productId);
        }catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return product;
    }
    
    public static boolean insertProduct(Product product) {
        if (ProductDAO.getProductById(product.getProductid()) != null) {
            return false;
        }
        Session session = HibernateUtil.getSessionFactory().openSession();
        boolean kq = false;
        try {
            session.beginTransaction();
            session.save(product);
            session.getTransaction().commit();
            kq = true;
        } catch (HibernateException ex) {
            System.err.println(ex);
            session.getTransaction().rollback();
            kq  =false;
        } finally {
            session.close();
        }
        return kq;
    }
    
     public static boolean updateProduct(Product product) { 
         if (ProductDAO.getProductById(product.getProductid()) != null) {
            return false;
        }
        Session session = HibernateUtil.getSessionFactory().openSession();
        boolean kq = false;
        try {
            session.beginTransaction();
            session.update(product);
            session.getTransaction().commit();
            kq = true;
        } catch (HibernateException ex) {
            System.err.println(ex);
            session.getTransaction().rollback();
            kq  =false;
        } finally {
            session.close();
        }
        return kq;
     }
     
     public static boolean deleteProduct(Product product) { 
         if (ProductDAO.getProductById(product.getProductid()) == null) {
            return false;
        }
        Session session = HibernateUtil.getSessionFactory().openSession();
        boolean kq = false;
        try {
            session.beginTransaction();
            session.delete(product);
            session.getTransaction().commit();
            kq = true;
        } catch (HibernateException ex) {
            System.err.println(ex);
            session.getTransaction().rollback();
            kq  =false;
        } finally {
            session.close();
        }
        return kq;
     }
}
